""" System imports """
import logging

""" Other modules """
import unittest

""" Game """
from items import *

""" Constants """

""" Set up logging """
_log = logging.getLogger("epicquest.inventory")

class Inventory():
    
    def __init__(self):
        self.items = []
    
    """ Add item to inventory. The count is the number to add
    """    
    def addItem(self, itm):
        for item in self.items:
            if item.name == itm.name:
                item.count = item.count + itm.count
                return
        _log.debug("Received " + str(itm.count) + " '" + itm.name + "'")
        self.items.append(itm)
        
    """ Pass in an item to remove. The count is the number to remove
    """
    def removeItem(self, itm):
        for item in self.items:
            if item.name == itm.name:
                item.count = item.count - itm.count
                if item.count <= 0:
                    self.items.remove(item)
                    _log.debug("Lost " + str(itm.count) + " '" + itm.name + "'")
                return itm
        
    """ Returns true if a given item is in the inventory.
        If mustHaveEnough is True, the inventory must contain 
        itm.count or more of an item for it to return True
    """
    def hasItem(self, itm, mustHaveEnough=False):
        for item in self.items:
            if item.name == itm.name:
                if mustHaveEnough:
                    return item.count >= itm.count
                else:
                    return True
               
    """ Returns a handle to the item in the inventory
    """ 
    def getItem(self, itm):
        for item in self.items:
            if item.name == itm.name:
                return item
        return None
    
    """ Returns the number of a given item the inventory contains
    """
    def countItem(self, itm):
        if not self.hasItem(itm):
            return 0
        
        item = self.getItem(itm)
        return item.count
    
    def giveItem(self, itm, target):
        if self.hasItem(itm, True):
            transient_itm = self.removeItem(itm)
            target.addItem(transient_itm)
            _log.debug("Giving " + itm.name + " to " + str(target))
        return False
        

class InventoryTest(unittest.TestCase):
        
    def setUp(self):
        pass
    
    def test_additem(self):
        #Set up a blank inventory
        self.inventory = Inventory()
        
        # Add some money
        self.inventory.addItem(Money())
        self.assertTrue(self.inventory.hasItem(Money()))
        self.assertFalse(self.inventory.hasItem(Money(count=2), True))
        
        self.inventory.addItem(Money())
        self.assertEqual(self.inventory.countItem(Money()), 2)
        
    def test_removeitem(self):
        self.inventory = Inventory()
        
        self.inventory.addItem(Item(count=2))
        self.assertTrue(self.inventory.hasItem(Item()))
        
        self.inventory.removeItem(Item())
        self.assertTrue(self.inventory.hasItem(Item()))
        self.assertEqual(self.inventory.countItem(Item()), 1)
        
        self.inventory.removeItem(Item())
        self.assertFalse(self.inventory.hasItem(Item()))
        
if __name__ == '__main__':
    unittest.main()
    
    

